using System.Collections.Generic;
using System.Threading.Tasks;
using CoreApp.DBAccess;
using CoreApp.DBModel;
using CoreApp.Utility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CoreApp.WebApi.Controllers
{
    [Route("api/Article/[action]")]
    [ApiController]
    public class ArticleController : ControllerBase
    {
        private readonly IArticleRepository articleRepository;
        public ArticleController(IArticleRepository _articleRepository)
        {
            articleRepository = _articleRepository;
        }

        [HttpGet]
        [Authorize]
        public async Task<ActionResult<IEnumerable<ArticleInfo>>> List()
        {
            Dictionary<string, string> dic=new Dictionary<string, string>();
            List<ArticleInfo> list = await articleRepository.GetListByParams(dic);
            //NLogHelper.Info("博客数量："+list.Count);
            return list;
        }

        [HttpPost]
        [Authorize]
        [ProducesResponseType(StatusCodes.Status404NotFound)]        
        public async Task<ActionResult<ArticleInfo>> Get(int id)
        {
            ArticleInfo article =null;
            if(id>0)
                article = await articleRepository.QueryArticle(id);

            if(article == null)
                return NotFound();

            return article;
        }
    }
}